package com.ceba.test.modules.usermanage.dao;

import abc.ID;
import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.XObject;
import abc.data.Sql;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.base.constants.CustomStampConstants;
import com.ceba.test.base.utils.StringUtil;
import com.ceba.test.modules.usermanage.entity.GSignatureMemory;

public class GSignatureMemoryDao extends IBaseDao {

    /**
     * 增加
     * @param
     * @param
     * @return
     */
    public boolean addGSignature(GSignatureMemory gSignatureMemory)throws Exception{
        IStore store = StoreManager.create("g_signature_memory");
        XObject object = new XObject("g_signature_memory");
        object.put("ID", ID.getGuid());
        object.put("DIGITAL_SIGNATURE_ID",gSignatureMemory.getDigitalSignatureId());
        object.put("QIFENG_SIGNATURE_ID",gSignatureMemory.getQifengSignatureId());
        object.put("REPORT_MERGE_ID",gSignatureMemory.getReportMergeId());
        object.put("ORIGINAL_DOCUMENT",gSignatureMemory.getOriginalDocument());
        object.put("USER_ID",gSignatureMemory.getUserId());
        object.put("CORFLAG",gSignatureMemory.getCorFlag());
        //object.put("WATERMARK_IDS",gSignatureMemory.getWatermarkIds());
        //object.put("OTHER_STAMP_IDS",gSignatureMemory.getOtherStampIds());
        object.setCreateTime();
        if (store.insert(object) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改
     * @param gSignatureMemory
     * @return
     */
    public boolean updateGSignature(GSignatureMemory gSignatureMemory) throws Exception{
        StringBuffer stringBuffer = new StringBuffer("update g_signature_memory set ");
        if(!StringUtil.isEmptyString(gSignatureMemory.getDigitalSignatureId())){
            stringBuffer.append("DIGITAL_SIGNATURE_ID='"+gSignatureMemory.getDigitalSignatureId()+"',");
        } else {
            stringBuffer.append("DIGITAL_SIGNATURE_ID='"+ CustomStampConstants.NO_SIGN+"',");
        }
        if(!StringUtil.isEmptyString(gSignatureMemory.getQifengSignatureId())){
            stringBuffer.append("QIFENG_SIGNATURE_ID = '"+gSignatureMemory.getQifengSignatureId()+"',");
        } else {
            stringBuffer.append("QIFENG_SIGNATURE_ID = '',");
        }
        if(!StringUtil.isEmptyString(gSignatureMemory.getReportMergeId())){
            stringBuffer.append( "REPORT_MERGE_ID='"+gSignatureMemory.getReportMergeId()+"',");
        }
        if(!StringUtil.isEmptyString(gSignatureMemory.getOriginalDocument())){
            stringBuffer.append("ORIGINAL_DOCUMENT='"+gSignatureMemory.getOriginalDocument()+"',");
        } else {
            stringBuffer.append("ORIGINAL_DOCUMENT='',");
        }
        if(!StringUtil.isEmptyString(gSignatureMemory.getCorFlag())){
            stringBuffer.append("CORFLAG='"+gSignatureMemory.getCorFlag()+"'");
        }
/*
        if(!StringUtil.isEmptyString(gSignatureMemory.getWatermarkIds())){
            stringBuffer.append("WATERMARK_IDS='"+gSignatureMemory.getWatermarkIds()+"'");
        } else {
            stringBuffer.append("WATERMARK_IDS=''");
        }
        if(!StringUtil.isEmptyString(gSignatureMemory.getOtherStampIds())){
            stringBuffer.append("OTHER_STAMP_IDS='"+gSignatureMemory.getOtherStampIds()+"'");
        } else {
            stringBuffer.append("OTHER_STAMP_IDS=''");
        }
*/
        if(stringBuffer.substring(stringBuffer.length()-1,stringBuffer.length()).equals(",")){
            stringBuffer = new StringBuffer(stringBuffer.substring(0,stringBuffer.length()-1));
        }
        stringBuffer.append("where ID='"+gSignatureMemory.getId()+"'");
//        if(StringUtil.isEmptyString(gSignatureMemory.getDigitalSignatureId())
//                &&StringUtil.isEmptyString(gSignatureMemory.getQifengSignatureId())
//                &&StringUtil.isEmptyString(gSignatureMemory.getReportMergeId())
//                && StringUtil.isEmptyString(gSignatureMemory.getOriginalDocument())
//                && StringUtil.isEmptyString(gSignatureMemory.getCorFlag())){
//            return true;
//        }
        Sql sql = new Sql(stringBuffer.toString());
        boolean sucess=  SqlSessionManager.execute(sql);
        return sucess;
    }

    /**
     * 查询
     * @param userId
     * @param corFlag
     * @return
     */
    public GSignatureMemory queryGSignature(String userId,String corFlag)throws Exception{
        Sql sql = new Sql("select DIGITAL_SIGNATURE_ID,ORIGINAL_DOCUMENT,QIFENG_SIGNATURE_ID,REPORT_MERGE_ID,ID,USER_ID,CORFLAG from g_signature_memory where USER_ID='"+userId+"' ORDER BY CREATE_TIME DESC");
        DataTable dataTable = SqlSessionManager.getDataTable(sql);
        GSignatureMemory gSignatureMemory = new GSignatureMemory();
        if(dataTable!=null && dataTable.getRowCount()>0){
            DataRow dataRow = dataTable.getRow(0);
            gSignatureMemory.setCorFlag(dataRow.getString("CORFLAG"));
            gSignatureMemory.setId(dataRow.getString("ID"));
            gSignatureMemory.setDigitalSignatureId(dataRow.getString("DIGITAL_SIGNATURE_ID"));
            gSignatureMemory.setQifengSignatureId(dataRow.getString("QIFENG_SIGNATURE_ID"));
            gSignatureMemory.setOriginalDocument(dataRow.getString("ORIGINAL_DOCUMENT"));
            gSignatureMemory.setReportMergeId(dataRow.getString("REPORT_MERGE_ID"));
            gSignatureMemory.setUserId(dataRow.getString("USER_ID"));
            //gSignatureMemory.setWatermarkIds(dataRow.getString("WATERMARK_IDS"));
            //gSignatureMemory.setOtherStampIds(dataRow.getString("OTHER_STAMP_IDS"));
        }
        return gSignatureMemory;
    }
}
